package com.superdry.fragment;

import java.util.List;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;

import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.drysurper.R;
import com.google.gson.JsonArray;
import com.superdry.activity.ChooseClothTypeActivity;
import com.superdry.activity.OrderInfoActivity;
import com.superdry.activity.RegisterActivity;
import com.superdry.adapter.OrderListAdapter;
import com.superdry.json.Order;
import com.superdry.json.OrderListItemBean;
import com.superdry.json.Simple;
import com.superdry.utils.GlobalConst;
import com.superdry.utils.SPHelper;
import com.superdry.utils.StringUtils;
import com.superdry.utils.ToastHelper;

public class Fragment_Order extends Fragment implements OnClickListener {

	ScrollView no_order;
	LinearLayout order_exist;
	// 有订单时的view
	ListView order_list;
	OrderListAdapter adapter;
	// 无订单时的view
	Button place_order;

	private AbHttpUtil mAbHttpUtil = null;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View settingLayout = inflater.inflate(R.layout.fragment_order, container, false);
		initViews(settingLayout);
		return settingLayout;
	}

	/**
	 * 初始化 view
	 */
	private void initViews(View settingLayout) {
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(getActivity());
		mAbHttpUtil.setTimeout(10000);

		order_exist = (LinearLayout) settingLayout.findViewById(R.id.order_exist);
		no_order = (ScrollView) settingLayout.findViewById(R.id.no_order);

		// 没有定单view
		place_order = (Button) settingLayout.findViewById(R.id.place_order);
		place_order.setOnClickListener(this);

		// 有订单的页面
		order_list = (ListView) settingLayout.findViewById(R.id.order_list);
		judgeViewToShow();
	}

	/**
	 * 判断要显示的view
	 */
	private void judgeViewToShow() {

		if (SPHelper.getUserInfo(getActivity()) == null) {
			// 未登入状态 显示无订单
			showNoOrderView();
			ToastHelper.show(getActivity(), "没有用户信息");
			return;
		}
		AbRequestParams params = new AbRequestParams();
		// params.put("userID", "25");
		int id = SPHelper.getUserInfo(getActivity()).getUserID();
		params.put("userID", id + "");
		mAbHttpUtil.post(GlobalConst.GET_ORDER_EXIST, params, new AbStringHttpResponseListener() {

			@Override
			public void onSuccess(int statusCode, String content) {

				List<Simple> lists = JSONArray.parseArray(content, Simple.class);
				if (lists != null && lists.get(0).getI() == 0) {
					showNoOrderView();
					return;
				}
				List<OrderListItemBean> orders = JSONArray.parseArray(content, OrderListItemBean.class);
				if (orders == null || orders.size() == 0) {
					showNoOrderView();
					return;
				}
				if (orders.size() > 0) {
					showOrderExistView(orders);
					return;
				}
				OrderListItemBean order = orders.get(0);
				if (!StringUtils.isEmpty(order.getOrderNO())) {
					showOrderExistView(orders);
				} else {
					showNoOrderView();
				}
			}

			@Override
			public void onStart() {
				System.out.println();
			}

			@Override
			public void onFinish() {

			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
				ToastHelper.show(getActivity(), "获取订单信息失败");
			}

		});

	}

	@Override
	public void onHiddenChanged(boolean hidden) {

		judgeViewToShow();
	}

	/**
	 * 显示无订单页面
	 */
	private void showNoOrderView() {
		clearViews();
		no_order.setVisibility(View.VISIBLE);
		order_exist.setVisibility(View.GONE);
	}

	/**
	 * 显示有订单页面
	 */
	private void showOrderExistView(List<OrderListItemBean> orders) {
		clearViews();
		order_exist.setVisibility(View.VISIBLE);
		no_order.setVisibility(View.GONE);
		adapter = new OrderListAdapter(getActivity(), orders);
		order_list.setAdapter(adapter);
		order_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
				AbRequestParams params = new AbRequestParams();
				params.put("orderID", adapter.getItem(position).getOrderID());
				mAbHttpUtil.post(GlobalConst.GET_ORDER_INFO, params, new AbStringHttpResponseListener() {

					@Override
					public void onStart() {

					}

					@Override
					public void onFinish() {

					}

					@Override
					public void onFailure(int statusCode, String content, Throwable error) {
						System.out.println(content);
					}

					@Override
					public void onSuccess(int statusCode, String content) {
						System.out.println(content);
					}
				});
				OrderListItemBean item = adapter.getItem(position);
				Intent intent = new Intent(getActivity(), OrderInfoActivity.class);
				intent.putExtra("order_info", item);
				startActivity(intent);
			}

		});
	}

	/**
	 * 清楚之前的显示状态
	 */
	private void clearViews() {
		order_exist.setVisibility(View.GONE);
		no_order.setVisibility(View.GONE);
	}

	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.place_order:
			// 一键下单
			Intent intent = null;
			// 判断用户是否登入 没有登入则跳入 注册页面 登入后 直接进入下单界面
			if (SPHelper.getUserInfo(getActivity()) == null) {
				intent = new Intent(getActivity(), RegisterActivity.class);
			} else {
				intent = new Intent(getActivity(), ChooseClothTypeActivity.class);
			}
			startActivity(intent);
			break;

		default:
			break;
		}
	}
}
